#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void selectSort(void * pAddr, int eleSize, int len, int(*comp)(void*, void*)){
    char *temp = malloc(eleSize);
    for(int i=0;i<len;i++)
    {
        int minOrMax = i;  //定义最小值 或者 最大值 下标
        for(int j=i+1;j<len;j++)
        {
            // 定义出j元素地址
            char * p = (char *)pAddr + eleSize*j;
            char * pMinOrMax = (char *)pAddr + eleSize *minOrMax;

            // if( pAddr[j] < pAddr[minOrMax])
            if( comp(p, pMinOrMax) )
            {
                minOrMax = j;   //更新最小值或者最大值的下标
            }
        }

        // 不等，交换
        if(minOrMax != i){
            // 交换下标元素
            char * pI = (char*)pAddr + i*eleSize;
            char * pMinOrMax = (char*)pAddr + minOrMax*eleSize;

            // char *temp = malloc(eleSize);
            memcpy(temp, pI, eleSize);
            memcpy(pI, pMinOrMax, eleSize);
            memcpy(pMinOrMax, temp, eleSize);
        }   

    }

    if (temp !=NULL){
        free(temp);
        temp=NULL;
    }
}

int comp(void * data1, void * data2){
    int * num1 = data1;
    int * num2 = data2;

    if(num1 < num2){
        return 1;
    }

    return 0;
}


void test1(){
    int arr[] = {10,20,50,40,69, 15};

    int len = sizeof(arr)/sizeof(int);
    selectSort(arr, sizeof(int), len, comp);

    for(int i=0;i<len;i++){
        printf("%d\n", arr[i]);
    }
}


int main(){

    test1();

    return 0;
}